{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获得前n个主成分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.empty((100,2))\n",
    "x[:, 0] = np.random.uniform(0., 100., size=100)\n",
    "x[:, 1] = 0.75 * x[:, 0] + 3. + np.random.normal(0, 10., size=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAHd5JREFUeJzt3X+QXeV93/H3h9ViLzjpSmahYoUinFKIUwqydzCJOh4bTDDGY+3YxjWxU6VDRv+krR0z2HLbae1OWislju3OeEg1JonSULACWDBOasoIedJ4BoUVAmMbVGEbhBYVrQ3CP1DMSv72j3sWrlb3xzn33nPuOed+XjOa3Xv37D3P4S7f89zv832eRxGBmZlV32nDboCZmQ2GA7qZWU04oJuZ1YQDuplZTTigm5nVhAO6mVlNOKCbmdWEA7qZWU04oJuZ1cSKIk921llnxbp164o8pZlZ5e3du/cHETHV7bhCA/q6deuYm5sr8pRmZpUn6ek0xznlYmZWEw7oZmY14YBuZlYTDuhmZjXhgG5mVhOpqlwk/R7wO0AAjwH/ElgN3AGsAh4GfisiXs6pnWZmpbdz3zw337efZ48e49zJCW66+kJm108Xdn5127FI0jTwt8AbI+KYpB3AXwPvAu6OiDsk/THwaETc0um1ZmZmwmWLZlZHO/fN88m7H+PY4olXnhsfE2eevoIXjy32Fe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UnKkj8APpdc9wvADUNpVb6+AHwtIi4CLqFx/bV+vyVNA/8GmImIfwKM0djprK7v958B71z2XLv3+BrgguTfZuCWXk5YyoAOXAY8GRHfi4iXgTuAjUNuU24i4nBEPJx8/2Ma/3NP07jm7clh24HZ4bQwP5LWANcCX0oeC7gCuDM5pHbXLekXgbcCtwJExMsRcZQReL9pLNk9IWkFcAZwmJq+3xHxN8Dzy55u9x5vBP48Gh4EJiWtznrOsgb0aeCZpseHkudqT9I6YD2wBzgnIg5DI+gDZw+vZbn5PPBx4OfJ49cDR5MtDqGe7/0bgAXgT5NU05cknUnN3++ImAf+EDhII5C/COyl/u93s3bv8UBiXlkDulo8V/tyHEmvA+4CPhoRPxp2e/Im6d3AkYjY2/x0i0Pr9t6vAN4E3BIR64GfUrP0SitJvngjcD5wLnAmjVTDcnV7v9MYyN99WQP6IeC8psdrgGeH1JZCSBqnEcxvi4i7k6efW/rYlXw9Mqz25WQD8B5JT9FIq11Bo8c+mXwkh3q+94eAQxGxJ3l8J40AX/f3+x3A9yNiISIWgbuBX6f+73ezdu/xQGJeWQP6Q8AFyej36TQGTu4dcptyk+SNbwUej4g/avrRvcCm5PtNwD1Fty1PEfHJiFgTEetovMcPRMSHgN3A+5PD6njd/w94RtLSjuZXAt+h5u83jVTL5ZLOSP7ml6671u/3Mu3e43uBf5FUu1wOvLiUmskkIkr5D3gX8H+B7wL/btjtyfla/xmNj1ffBB5J/r2LRj55F3Ag+bpq2G3N8b/B24CvJt+/Afg74EngL4HXDLt9OVzvpcBc8p7vBFaOwvsNfBp4AvgW8D+A19T1/QZupzFWsEijB35Du/eYRsrli0m8e4xGJVDmc3qmqJlZTZQ15WJmZhk5oJuZ1YQDuplZTTigm5nVhAO6mVlNOKCbmdWEA7qZWU04oJuZ1cT/B4cDozjEahJ+AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x19b4795b390>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x[:,0], x[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def demean(x):\n",
    "    return x - np.mean(x, axis=0)  # 在列上求mean,即 样本 - 各个特征的均值\n",
    "\n",
    "x = demean(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAHgRJREFUeJzt3X+QXWWd5/H3h6bFdqZmGiQ60AmTlBUZQdQ4XcBUqrYUYUFFkkVnFtdxKIfalFVYq7sOkgxbo9Y6ZVxqlilrZtxNjVZhLWtEcUNGnUWQWFtLCdoxIEZgiTpCOqy0K9GZTZRO8t0/7mlyu3Puz3POvefH51XVlb6nT9/zHBK+57nf5/s8jyICMzOrv9PG3QAzMxsNB3wzs4ZwwDczawgHfDOzhnDANzNrCAd8M7OGcMA3M2sIB3wzs4ZwwDcza4jTx92AdmeffXasXbt23M0wM6uUvXv3/jQiVvU6r1QBf+3atczNzY27GWZmlSLpx/2c55SOmVlDOOCbmTWEA76ZWUM44JuZNYQDvplZQ+RWpSNpApgD5iPiaknrgJ3AWcB3gHdHxPN5Xc/MrGp27Zvn1nue4NDho5w7PcVNV57P5g0zI7t+nmWZ7wceA34jef0J4LaI2CnpPwM3AJ/K8XpmZpWxa9882770KEcXjwMwf/go2770KHM//hl7Hl8YyUMgl5SOpNXAW4G/TV4LuAz4YnLK7cDmPK5lZlZFt97zxAvBfsnRxePc8eBTzB8+SnDyIbBr33whbcgrh/+XwIeAE8nrlwKHI+JY8vogMLrPLWZmJXPo8NHU4yt3FT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BW5M7ncr8PWIWA98PXldR+8HHmt7/QngtuS+nwNuGEurcuSAP7jbgA+xfFP2TcBnk41fHgSmJZ0zltYVICK+FhHHkpcP0tqjGFr3vTMifhURP6K1w9nF42hjQS4GDkTEDyPieWAnrXuupYh4JiK+k3z/j7SC3wyte749Oe12YPN4WlgcSauBtwJ/m7wWcBnwxeSUWty3A/4AJF0DzEfEIyt+NAM83fb6YHKsjv4Y+Pvk+7rfd93vryNJa4ENwEPAy5f2pE7+fFnn36ysv6TVkTuRvH4pcLito1OLv/tab4AyDEn3Ab+V8qNbgD8F/nnar6Ucq1T5U7f7joi7k3NuofWx/46lX0s5v1L33UPd7y+VpF8H7gI+EBG/aHV260vS1cCzEbFX0huWDqecWvm/ewf8FSLi8rTjki4C1gGPJP8DrAa+I+liWk//NW2nrwYOFdzUXHW67yWSrgeuBt4UJ2t5K3/fPdT9/k4haZJWsL8jIr6UHP6JpHMi4pkkVfns+FpYiI3ANZLeArwY+A1aPf5pSacnvfxa/N07pdOniHg0Il6WbNa+llYweH1E/B9gN/BHSbXOpcDPlz4C14Gkq4CbgWsi4kjbj3YD10k6Q9I6WoPW3xpHGwvybWB9Uq3xIloD1LvH3KbCJHnrTwOPRcR/avvRbuD65PvrgbtH3bYiRcS2iFid/H99HXB/RLwL2AO8IzmtFvftHn4+vgq8hdag5RHgPeNtTu7+CjgDuDf5dPNgRLw3IvZLuhP4Pq1Uz40RcXyM7cxVRByT9D7gHmAC+ExE7B9zs4q0EXg38Kikh5NjfwpsB+6UdAOt6rTfH1P7Ru1mYKekjwH7aD0MK80zbc3MGsIpHTOzhnDANzNrCAd8M7OGcMA3M2sIB3wzs4ZwwDczawgHfDOzhnDANzNriP8P8aIbc/LxgvsAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x19b479a22e8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x_demean = demean(x)\n",
    "plt.scatter(x_demean[:,0], x_demean[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(w, x):\n",
    "    '''目标函数'''\n",
    "    return np.sum((x.dot(w))**2)/len(x)\n",
    "\n",
    "def df(w, x):\n",
    "    return x.T.dot(x.dot(w)) * 2./len(x)\n",
    "\n",
    "def direction(w):\n",
    "    '''将一个向量变成单位向量'''\n",
    "    return w / np.linalg.norm(w)  # norm 求向量的模(实际上是根据ord返回范数)\n",
    "\n",
    "def first_component(x, initial_w, eta, n_iters=1e4, epsilon=1e-8):\n",
    "    \n",
    "    w = direction(initial_w)\n",
    "    cur_iter = 0\n",
    "    \n",
    "    while cur_iter < n_iters:\n",
    "        gradient = df(w, x)\n",
    "        last_w = w  # 我们投影的时候是投影在方向向量w上面的,即需要将w变成单位方向向量\n",
    "        w = w + eta * gradient\n",
    "        w = direction(w)   # 注意归一化为单位方向向量 [1]\n",
    "        if (abs(f(w, x) - f(last_w, x)) < epsilon):\n",
    "            break\n",
    "        cur_iter += 1\n",
    "    \n",
    "    return w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.79119843, 0.61155952])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "initial_w = np.random.random(x.shape[1])\n",
    "eta = 0.01\n",
    "w = first_component(x, initial_w, eta)\n",
    "w"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### $X^{(i)}\\cdot w = \\left \\| X_{project}^{(i)} \\right \\|$\n",
    "\n",
    "$X_{project}^{(i)}=\\left \\| X_{project}^{(i)} \\right \\| \\cdot w$\n",
    "\n",
    "第二主成分上的投影向量 $=X^{(i)} - X_{project}^{(i)}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(100, 2)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# x2 = np.empty(x.shape)\n",
    "# for i in range(len(x)):\n",
    "#     x2[i] = x[i] - x[i].dot(w) * w\n",
    "\n",
    "# 求x2的向量化方法\n",
    "x2 = x - x.dot(w).reshape(-1,1) * w  #### 后面是(100,2) - (100,1)*(2, )\n",
    "# w.shape   ###(2, )\n",
    "# x.shape   ###(100, 2)\n",
    "# x.dot(w).reshape(-1, 1).shape  ###(100,1)\n",
    "# x2 = x - x.dot(w) * w   #### 这里报错是因为 (100,2) - (100,) *(2, )\n",
    "x2.shape   ####(100, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAFp5JREFUeJzt3X9s3PV9x/HXG8+tDtTKIAIlV9xkLPVW6tbeLH4oWkVZi6FVy5GtDRlMSK2aTip/BCpL8cogKakSLWqJNG3dgoaKFgpJaXDThhF+VUOLSFtHdnACzUj4EXKJGqPE+8WJHs57f/guPdt3vu/Z971fn+dDsmLffe++H+Pkxcef7/v7/pi7CwDQ+s6r9wAAALVB4ANAIAh8AAgEgQ8AgSDwASAQBD4ABILAB4BAEPgAEAgCHwAC8Xv1HkChiy++2JcsWVLvYQBAU9m/f//b7r6o3HENFfhLlizR8PBwvYcBAE3FzN6MchxLOgAQCAIfAAJB4ANAIAh8AAgEgQ8AgWioKp35GhpJa/OewzoxkdHijoQG+ruU6k3We1gA0FCaPvCHRtIa3DmmTHZSkpSeyGhw55gkEfoAUKDpl3Q27zl8LuzzMtlJbd5zuE4jAoDG1PSBf2IiU9Hj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gwWMXmdkzZvZq7s8L6znGcgj86sjv7ftC4YNm9jFJt0q6UtKNkv4x3za6xT3g7j25jyfrPZi45H6W/yDpJkkfk7Qq9zMPzadzP+umKU+cpx9o6t9xobWSnnP3ZZKey33dsAj8Kphjb9+bJT3m7u+6++uSjki6qrajQ4yuknTE3V9z999KekxTP3O0IHd/QdLMNu83S3o49/nDklI1HVSFCPx4JSW9VfD18dxjre5OM3sp9ytwQ/+Ku0Ch/nwLuaSnzWy/ma2u92Dq4FJ3PylJuT8vqfN45rSgfvghMbNnJX2oyFPfcveflHpZkceavixqrv8Wkr4v6X5NfZ/3S/qupK/UbnQ11ZI/3wotd/cTZnaJpGfM7Ne5mTAaEIEf0Xz29tXUjO/ygq8/LOlEdUZUP1H/W5jZg5J+FvNw6qklf76VcPcTuT9PmdkTmlrmCinwf2Nml7n7STO7TNKpeg9oLizpxGuXpFvN7P1mtlRTe/v+ss5jilXuL33eLZq6oN2qfiVpmZktNbP3aeoC/a46j6lmzOwCM/tA/nNJN6i1f97F7JJ0R+7zOySV+m2/ITDDr4JSe/u6+yEz2yHpZUnvSfqGu0/Wc6w18Hdm1qOppY03JH29vsOJj7u/Z2Z3StojqU3SQ+5+qM7DqqVLJT1hZtJUlvzQ3Z+q75DiY2aPSrpO0sVmdlzSfZI2SdphZl+VdEzSl+o3wvK40xYAAsGSDgAEgsAHgEAQ+AAQCAIfAAJB4ANAIAh8AAgEgQ8AgSDwASAQ/w9Q7CLkRYBoYAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x19b48ade668>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x2[:,0], x2[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.61155478,  0.79120209])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 复用第一主成分的代码, 求出第二主成分方向向量(即系数)\n",
    "w2 = first_component(x2, initial_w, eta)\n",
    "w2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.989241233572695e-06"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w.dot(w2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
